
package com.ikamobile.intentexp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mResult;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mResult = (TextView) findViewById(R.id.result);
        IntentFilter filter = new IntentFilter(ExpReceiver.POST_RESULT);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mResult.append(intent.getStringExtra(ExpReceiver.EXTRA_RESULT) + "\n");
            }
        }, filter);

        Intent intent = getIntent();
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            mResult.setText(intent.getDataString());
        }
    }

    public void onClick(View view) {
        mResult.setText(null);
        String[] actions = {
                ExpReceiver.FILTER_ACTION_ONLY, ExpReceiver.FILTER_ACTION_EMPTY_DATA,
                ExpReceiver.FILTER_ACTION_DATA_SCHEME, ExpReceiver.FILTER_ACTION_CATEGORY,
        };
        for (String a : actions)
            sendBroadcasts(a);
    }

    private void sendBroadcasts(String action) {
        Uri data = Uri.parse(ExpReceiver.URI_SCHEME + "://123");

        Intent intent = new Intent(action);
        sendBroadcast(intent);

        intent = new Intent(action, data);
        sendBroadcast(intent);

        intent = new Intent(action);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(intent);
    }
}
